﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Model;
using Avatar.Company;
using Avatar.Common;
using AvatarHelper;

public partial class JobSearch : System.Web.UI.Page
{
    int _page;
    string _key = string.Empty;
    int _industryID = 0;
    int _jobTypeID = 1;
    int _day = 31;
    protected void Page_Load(object sender, EventArgs e)
    {
        using (var _adc = DBHelper.GetDataContext())
        {
            if (!IsPostBack)
            {
                ddl_JobType.DataSource = _adc.JobType.ToList();
                ddl_JobType.DataBind();
                ddl_PublishDateRange.DataSource = _adc.PublishDateRange.ToList();
                ddl_PublishDateRange.DataBind();
            }

            _key = Request.QueryString["key"] == null ? "" : Server.UrlDecode(Request.QueryString["key"].ToString());
            if (!string.IsNullOrEmpty(_key))
            {
                //txt_KeyWord.Text = _key;
                txt_CurrentKeyWord.Text = "<font class='org12'>当前搜索：</font>" + _key;
            }
            try
            {
                _page = Request.QueryString["Page"] == null ? 0 : Convert.ToInt32(Request.QueryString["Page"].ToString());
                _industryID = Request.QueryString["Industry"] == null ? 0 : Convert.ToInt32(Request.QueryString["Industry"].ToString());
                _jobTypeID = Request.QueryString["JobTypeID"] == null ? _jobTypeID : Convert.ToInt32(Request.QueryString["JobTypeID"].ToString());
                _day = Request.QueryString["Day"] == null ? _day : Convert.ToInt32(Request.QueryString["Day"].ToString());

                if (!IsPostBack)
                {
                    var _industry = _adc.Industry.FirstOrDefault(i => i.ID == _industryID);
                    if (_industry != null)
                    {
                        lbl_IndustrySelect.Text = "[" + _industry.Name + "]";
                        txt_IndustryID.Value = _industry.ID.ToString();
                    }
                    //ddl_JobType.SelectedValue = _jobTypeID.ToString();
                    //ddl_PublishDateRange.SelectedValue = _day.ToString();
                }
            }
            catch { }
        }
        this.Search(_page, _key, _industryID, _jobTypeID, _day);
    }
    protected void chk_All_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        try
        {
            _key = txt_KeyWord.Text;
            _industryID = string.IsNullOrEmpty(txt_IndustryID.Value) ? 0 : Convert.ToInt32(txt_IndustryID.Value);
            _jobTypeID = Convert.ToInt32(ddl_JobType.SelectedValue);
            _day = Convert.ToInt32(ddl_PublishDateRange.SelectedValue);

            Response.Redirect("./JobSearch.aspx?Key=" + Server.UrlEncode(_key) + "&Page=" + 0 + "&Industry=" + _industryID + "&JobTypeID=" + _jobTypeID + "&Day=" + _day);
        }
        catch (Exception _e)
        {
            Helper.ShowMassage(_e.Message);
        }
    }
    protected void btn_Up_Click(object sender, EventArgs e)
    {
        _page = Convert.ToInt32(lbl_Current.Text) > 0 ? Convert.ToInt32(lbl_Current.Text) - 1 : 0;
        if (_page != 0) _page -= 1;
        Response.Redirect("./JobSearch.aspx?Key=" + Server.UrlEncode(_key) + "&Page=" + _page + "&Industry=" + _industryID + "&JobTypeID=" + _jobTypeID + "&Day=" + _day);

    }
    protected void btn_Down_Click(object sender, EventArgs e)
    {
        _page = Convert.ToInt32(lbl_Current.Text) > 0 ? Convert.ToInt32(lbl_Current.Text) - 1 : 0;
        if (_page != Convert.ToInt32(lbl_Total.Text)) _page += 1;
        Response.Redirect("./JobSearch.aspx?Key=" + Server.UrlEncode(_key) + "&Page=" + _page + "&Industry=" + _industryID + "&JobTypeID=" + _jobTypeID + "&Day=" + _day);
    }
    protected void btn_Goto_Click(object sender, EventArgs e)
    {
        try
        {
            _page = string.IsNullOrEmpty(txt_Page.Text) ? 0 : Convert.ToInt32(txt_Page.Text) - 1;
            if (_page >= 0 && _page < Convert.ToInt32(lbl_Total.Text))
            {
                Response.Redirect("./JobSearch.aspx?Key=" + Server.UrlEncode(_key) + "&Page=" + _page + "&Industry=" + _industryID + "&JobTypeID=" + _jobTypeID + "&Day=" + _day);
            }
        }
        catch
        { }
    }
    protected void Search(int _page, string _key, int _industryID, int _jobTypeID, int _day)
    {
        if (_jobTypeID == 0) _jobTypeID = 1;
        var _dayRange = DateTime.Now.AddDays(Convert.ToDouble("-" + _day.ToString()));
        using (var _adc = DBHelper.GetDataContext())
        {
            if (_industryID == 0)
            {
                lbl_JobCount.Text = _adc.Job.Count(j => (j.Title.Contains(_key) || j.Detail.Contains(_key)) && j.JobTypeID == _jobTypeID && j.PublishDate >= _dayRange).ToString();
                ListView1.DataSource = _adc.Job.Where(j => (j.Title.Contains(_key) || j.Detail.Contains(_key)) && j.JobTypeID == _jobTypeID && j.PublishDate >= _dayRange).OrderByDescending(j => j.PublishDate).Skip(25 * _page).Take(25).ToList();
            }
            else if (_industryID != 0)
            {
                lbl_JobCount.Text = _adc.Job.Count(j => (j.Title.Contains(_key) || j.Detail.Contains(_key)) && j.IndustryID == _industryID && j.JobTypeID == _jobTypeID && j.PublishDate >= _dayRange).ToString();
                ListView1.DataSource = _adc.Job.Where(j => (j.Title.Contains(_key) || j.Detail.Contains(_key)) && j.IndustryID == _industryID && j.JobTypeID == _jobTypeID && j.PublishDate >= _dayRange).OrderByDescending(j => j.PublishDate).Skip(25 * _page).Take(25).ToList();
            }
            lbl_Current.Text = (_page + 1).ToString();
            lbl_Total.Text = Convert.ToInt32(lbl_JobCount.Text) > 25 ? (Convert.ToInt32(lbl_JobCount.Text) % 25 == 0 ? Convert.ToInt32(lbl_JobCount.Text) / 25 : Convert.ToInt32(lbl_JobCount.Text) / 25 + 1).ToString() : "0";
            ListView1.DataBind();
        }
    }
    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string GetDynamicContent(string contextKey)
    {
        return default(string);
    }
    protected void btn_IndustrySelect_Click(object sender, EventArgs e)
    {
        LinkButton _btn = sender as LinkButton;
        var _id = _btn.CommandArgument;
        lbl_IndustrySelect.Text = "[" + _btn.Text + "]";
        txt_IndustryID.Value = _id;
    }
    protected void btn_IndustryClear_Click(object sender, EventArgs e)
    {
        lbl_IndustrySelect.Text = string.Empty;
        txt_IndustryID.Value = string.Empty;
    }
}
